﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// WMS停用账号
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserCloseController : ControllerBase
    {
        private readonly ILogger<WMSUserCloseRequestDto> _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_qh;
        public readonly string con_yb;


        public UserCloseController(ILogger<WMSUserCloseRequestDto> logger,IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration)
        {
            _logger = logger;
            _WMSTokenCheckService = wmsTokenCheckService;
             con= _configuration.GetConnectionString("WMSDbConnection_ZH");
            con_qh = _configuration.GetConnectionString("WMSDbConnection");
            con_yb = _configuration.GetConnectionString("WMSDbConnection_YB");

        }

        /// <summary>
        /// WMS停用账号
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostUserClose([FromBody] WMSUserCloseRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            WMSUserCloseRequestDto result = new WMSUserCloseRequestDto();
            WMSUserCloseResponseDto re = new WMSUserCloseResponseDto();
            string user_id = strparm.user_id;
            string dept_code = strparm.dept_code;
            //框架查询方法
            //var data = _WMSTokenCheckService.GetTokenCheckData(supplier_code);

            try
            {
                if (dept_code=="")
                {
                    strMsg = "停用组织代码不能为空，请传《ZH》或《QH》或《YB》";
                }
                else
                {
                    if (dept_code == "ZH"|| dept_code == "QH" || dept_code == "YB")
                    {
                        string strcon = "";
                        if (dept_code == "YB")
                        {
                            strcon = con_yb;
                        }
                        else if (dept_code == "ZH")
                        {
                            strcon = con;
                        }
                        else
                        {
                            strcon = con_qh;
                        }
                        strSql = new StringBuilder();
                        strSql.Append(@"select  * from sys_user  where 1=1 and user_name='" + user_id + "'");

                        DataTable dt_barcodecheck = DB.Query(strSql.ToString(), strcon);
                        if (dt_barcodecheck.Rows.Count <= 0)
                        {
                            strMsg = "WMS系统不存在《" + user_id + "》账号信息";
                        }
                        else
                        {
                            strSql = new StringBuilder();
                            strSql.Append(@"update sys_user set status=1  where  user_name='" + user_id + "'");
                            strSqlList.Add(strSql.ToString());
                        }

                        if (strSqlList.Count > 0)
                        {
                            bool A = DB.ExecuteNoQueryTran(strSqlList, strcon);
                            if (!A)
                            {
                                strMsg = "停用失败,更新数据库失败";
                            }
                        }
                    }
                    else
                    {
                        strMsg = "停用组织代码有误，请传《ZH》或《QH》";
                    }
                }              
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

         


            if (strMsg=="")
            {
                re.status = "成功";
                re.code = "1";
                re.message = "";
            }
            else
            {
                re.status = "失败";
                re.code = "0";
                re.message = strMsg;
            }


            return new JsonResult(re);
        }





    }
}
